package com.banshou.installs.utils;

import android.content.Context;
import android.content.SharedPreferences;

/**
 * Created by cjq on 2018/1/2.
 * Email: stoic_yb@139.com
 * features:
 */

public class SharePreUtil {
    private static SharedPreferences sp;

    /**
     * 保存服务器的Token(登陆状态)
     */
    public static void saveToken(Context context,String value){
        if(sp == null){
            sp = context.getSharedPreferences("config",Context.MODE_PRIVATE);
        }
        sp.edit().putString("TOKEN",value).apply();
    }

    public static String getToken(Context context,String defValue){
        if (sp == null){
            sp = context.getSharedPreferences("config", Context.MODE_PRIVATE);
        }
        return sp.getString("TOKEN",defValue);
    }

    /**
     * 保存UserId
     */
    public static void setUserId(Context context, String value){
        if(sp == null){
            sp = context.getSharedPreferences("config",Context.MODE_PRIVATE);
        }
        sp.edit().putString("USERID",value).apply();
    }

    public static String getUserId(Context context, String defValue){
        if(sp == null){
            sp = context.getSharedPreferences("config",Context.MODE_PRIVATE);
        }
        return sp.getString("USERID",defValue);
    }

    /**
     * 保存TEl
     */
    public static void setTel(Context context, String value){
        if(sp == null){
            sp = context.getSharedPreferences("config",Context.MODE_PRIVATE);
        }
        sp.edit().putString("TEL",value).apply();
    }

    public static String getTel(Context context, String defValue){
        if(sp == null){
            sp = context.getSharedPreferences("config",Context.MODE_PRIVATE);
        }
        return sp.getString("TEL",defValue);
    }
    /**
     * 保存权限
     */
    public static void setPower(Context context, int value){
        if(sp == null){
            sp = context.getSharedPreferences("config",Context.MODE_PRIVATE);
        }
        sp.edit().putInt("POWER",value).apply();
    }
    public static Integer getPower(Context context, int defValue){
        if(sp == null){
            sp = context.getSharedPreferences("config",Context.MODE_PRIVATE);
        }
        return sp.getInt("POWER",defValue);
    }

    /**
     * name
     */
    public static void setName(Context context, String value){
        if(sp == null){
            sp = context.getSharedPreferences("config",Context.MODE_PRIVATE);
        }
        sp.edit().putString("NAME",value).apply();
    }
    public static String getName(Context context, String defValue){
        if(sp == null){
            sp = context.getSharedPreferences("config",Context.MODE_PRIVATE);
        }
        return sp.getString("NAME",defValue);
    }

    /**
     * company_id
     */
    public static void setCompanyId(Context context, String value){
        if(sp == null){
            sp = context.getSharedPreferences("config",Context.MODE_PRIVATE);
        }
        sp.edit().putString("COMPANYID",value).apply();
    }
    public static String getCOMPANYID(Context context, String defValue){
        if(sp == null){
            sp = context.getSharedPreferences("config",Context.MODE_PRIVATE);
        }
        return sp.getString("COMPANYID",defValue);
    }

    /**
     * company_name
     */
    public static void setCompanyName(Context context, String value){
        if(sp == null){
            sp = context.getSharedPreferences("config",Context.MODE_PRIVATE);
        }
        sp.edit().putString("COMPANYNAME",value).apply();
    }
    public static String getCompanyName(Context context, String defValue){
        if(sp == null){
            sp = context.getSharedPreferences("config",Context.MODE_PRIVATE);
        }
        return sp.getString("COMPANYNAME",defValue);
    }

    public static void clear(Context context){
        if(sp == null){
            sp = context.getSharedPreferences("config", Context.MODE_PRIVATE);
        }
        SharedPreferences.Editor editor = sp.edit();
        editor.clear();
        editor.apply();
    }
}
